package com.xin.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xin.dao.OrderSettingDao;
import com.xin.pojo.OrderSetting;
import com.xin.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author 夜天
 * @Date 2021/12/16 22:08
 * @Version 1.0.0
 * @description： 预约设置服务接口实现类
 */
@Service(interfaceClass = OrderSettingService.class)
// 开启事务
@Transactional
public  class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    // 批量添加
    @Override
    public void add(List<OrderSetting> data) {
        if(data != null && data.size() > 0){
            for (OrderSetting orderSetting : data) {
                //检查此数据（日期）是否存在
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(count > 0){
                    //已经存在，执行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else{
                    //不存在，执行添加操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }
    // 根据月份查询对应的预约设置数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        // 调用dao根据日期范围查询预约设置数据
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        // 返回值
        List<Map> result = new ArrayList<>();
        if(list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> map1 = new HashMap<>();
                map1.put("date",orderSetting.getOrderDate().getDate());// 获取日期
                map1.put("number",orderSetting.getNumber());
                map1.put("reservations",orderSetting.getReservations());
                result.add(map1);
            }
        }
        return result;
    }
    // 根据指定日期修改可预约人数
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        // 根据日期查询，是否已经进行了预约设置
        long count = orderSettingDao.findCountByOrderDate(orderDate);
        if(count > 0){
            // 当前日期已经进行了预约设置，需要进行更新操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            // 当前日期没有进行预约设置，需要进行添加操作
            orderSettingDao.add(orderSetting);
        }

    }
}
